package com.zsg.service.impl;

import com.zsg.dao.RoleMapper;
import com.zsg.pojo.Role;
import com.zsg.pojo.User;
import com.zsg.pojo.UserRole;
import com.zsg.service.RoleService;
import com.zsg.utils.Constant;
import com.zsg.utils.ResponseData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired(required = false)
    private RoleMapper roleMapper;

//    @Autowired
//    private ResponseData responseData;

    @Override
    public ResponseData addRole(Role role) {
        ResponseData responseData = new ResponseData();
        int re = roleMapper.addRole(role);

        if (re>0){
            responseData.setCode(Constant.SUCCESS);
            responseData.setMsg("添加成功");
            responseData.setData(null);
            return responseData;
        }
        responseData.setCode(Constant.FILED);
        responseData.setMsg("服务器错误");
        responseData.setData(null);
        return responseData;

    }

    @Override
    public ResponseData delRole(Role role) {
        ResponseData responseData = new ResponseData();
        if (role.getId() == null) {
            responseData.setCode(Constant.FILED);
            responseData.setMsg("角色ID不能为空");
            responseData.setData(null);
            return responseData;
        }

        int re = roleMapper.delRole(role);
        if (re>0){
            responseData.setCode(Constant.SUCCESS);
            responseData.setMsg("删除成功");
            responseData.setData(null);
            return responseData;
        }
        responseData.setCode(Constant.FILED);
        responseData.setMsg("服务器错误");
        responseData.setData(null);
        return responseData;

    }

    @Override
    public ResponseData updateRole(Role role) {
        ResponseData responseData = new ResponseData();
        if (role.getId() == null) {
            responseData.setCode(Constant.FILED);
            responseData.setMsg("角色ID不能为空");
            responseData.setData(null);
            return responseData;
        }
        //查询当前角色是否存在
        int count = roleMapper.getRole(role.getId());
        if (count==0){
            responseData.setCode(Constant.FILED);
            responseData.setMsg("当前角色不存在");
            return responseData;
        }


        int re = roleMapper.updateRole(role);
        if (re>0){
            responseData.setCode(Constant.SUCCESS);
            responseData.setMsg("修改成功");
            responseData.setData(null);
            return responseData;
        }
        responseData.setCode(Constant.FILED);
        responseData.setMsg("服务器错误");
        responseData.setData(null);
        return responseData;

    }

    @Override
    public ResponseData queryRole(Role role) {
        ResponseData responseData = new ResponseData();
        //查询角色
        Role role1 = roleMapper.queryRole(role);
        System.out.println(role1);
        responseData.setCode(Constant.SUCCESS);
        responseData.setMsg("查询成功");
        responseData.setData(role1);
        return responseData;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResponseData userBoundRole(int userID, List<Integer> roleId) {
        ResponseData responseData = new ResponseData();
        log.info("绑定角色");
        //先删除关于用户角色
        int re = roleMapper.delRoleById(userID);
        log.info("删除用户角色："+re+"条");
        //为用户添加角色
        int res = roleMapper.addUserRole(userID,roleId);
        log.info("添加用户角色："+res+"条");
        if (res>0){
            responseData.setCode(Constant.SUCCESS);
            responseData.setMsg(res+"个角色被添加");
            responseData.setData(null);
            return responseData;
        }
        responseData.setCode(Constant.FILED);
        responseData.setMsg("服务器错误");
        responseData.setData(null);
        return responseData;
    }

    @Override
    public ResponseData queryUserRole(User user) {
        ResponseData responseData = new ResponseData();
        User user1 = roleMapper.queryUserRole(user);
        responseData.setCode(Constant.SUCCESS);
        responseData.setMsg("查询成功");
        responseData.setData(user1);
        return responseData;
    }

    @Override
    public ResponseData delUserRole(UserRole userRole) {
        ResponseData responseData = new ResponseData();
        int re = roleMapper.delUserRole(userRole);
        if (re>0){
            responseData.setCode(Constant.SUCCESS);
            responseData.setMsg("取消成功");
            responseData.setData(null);
            return responseData;
        }
        responseData.setCode(Constant.FILED);
        responseData.setMsg("服务器错误");
        responseData.setData(null);
        return responseData;

    }
}
